package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PutMapping
    public Result update(@RequestBody Dept dept) {
        deptService.update(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    public Result dept(@PathVariable Integer id) {
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    @GetMapping
    public Result depts() {
//        List<Dept> list = deptService.findAll();
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    @DeleteMapping
    public Result delete(Integer id) {
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept) {
        deptService.add(dept);
        return Result.success();
    }

}
